package com.greyshark.lib.bluetooth.utils

import android.content.Context
import android.util.Log
import java.io.File
import java.text.SimpleDateFormat
import java.util.Date
import java.util.Locale

object LogUtils {
  //app日志
  private lateinit var logger: Logger

  //蓝牙日志
  private lateinit var bleLogger: Logger

  fun initLogger(context: Context, isWriteLog: Boolean, isRelease: Boolean) {
    logger = Logger.LoggerBuilder(context)
      .setIsWriteLog(isWriteLog)
      .setExpiredDay(5)
      .setFileDirPath(getDirPath(context, isRelease, "app"))
      .setPrefixFlag("APP")
      .build()

    bleLogger = Logger.LoggerBuilder(context)
      .setIsWriteLog(isWriteLog)
      .setExpiredDay(5)
      .setFileDirPath(getDirPath(context, isRelease, "ble"))
      .setPrefixFlag("BLE")
      .build()
  }

  /**
   * 获取文件存储路径
   */
  fun getDirPath(
    context: Context,
    isRelease: Boolean,
    key: String
  ): String {
    return if (isRelease) {
      context.filesDir.absolutePath + File.separator + "log" + File.separator + key
    } else {
      context.getExternalFilesDir("log" + File.separator + key)?.absolutePath ?: ""
    }
  }

  /*
  val pathList = LogUtils.getLogFilePath(
  this,
  false,
  "ble",
  longArrayOf(System.currentTimeMillis())
  )
   */

  fun getLogFilePath(
    context: Context,
    isRelease: Boolean,
    key: String,
    times: LongArray
  ): List<String?> {
    val array = arrayListOf<String?>()

    val dirPath = if (isRelease) {
      context.filesDir.absolutePath + File.separator + "log" + File.separator + key
    } else {
      context.getExternalFilesDir("log" + File.separator + key)?.absolutePath ?: ""
    }

    val dir = File(dirPath)
    if (!dir.exists()) {
      Log.i("LogUtils","dir is not exist")
        return List(times.size) {
        null
      }
    }

    val dateFormat = SimpleDateFormat("yyyy-MM-dd", Locale.getDefault())

    for (time in times) {
      val date = Date(time)
      val dateStr = dateFormat.format(date)
      val fileName = "${key}_${dateStr}.log"
      Log.i("LogUtils", "fileName = $fileName")
      val fullPath = "$dirPath${File.separator}$fileName"
      Log.i("LogUtils", "fullPath = $fullPath")
      val file = File(fullPath)
      if (file.exists()) {
        Log.i("LogUtils","file is not exist")
        array.add(fullPath)
      } else {
        array.add(null)
      }
    }

    return array
  }

  //region APP 日志

  fun appLogPath(): String {
    return logger.getLogFilePath()
  }

  @JvmStatic
  fun i(tag: String, msg: String) {
    Log.i(tag, msg)
    appLog(tag, msg)
  }

  @JvmStatic
  fun v(tag: String, msg: String) {
    Log.v(tag, msg)
    appLog(tag, msg)
  }

  @JvmStatic
  fun e(tag: String, msg: String) {
    Log.e(tag, msg)
    appLog(tag, msg)
  }

  @JvmStatic
  fun d(tag: String, msg: String) {
    Log.d(tag, msg)
    appLog(tag, msg)
  }

  @JvmStatic
  fun w(tag: String, msg: String) {
    Log.w(tag, msg)
    appLog(tag, msg)
  }


  @JvmStatic
  fun appLog(tag: String?, msg: String?) {
    bleLogger.writeFile(tag, msg)
  }
  //endregion

  //region 蓝牙日志
  fun bleLogPath(): String {
    return bleLogger.getLogFilePath()
  }

  @JvmStatic
  fun bleLog(tag: String?, msg: String?) {
    bleLogger.writeFile(tag, msg)
  }
  //endregion
}